<?php

class DataSelect extends Block{

	public $templatePath = __DIR__ ;
	public $templateFile = "DataSelect.tpl.std.html";
	
	public $file = "";
	public $list = "";
	public $separator=",";
	
	//Tag Attributes
	public $caption     = "DataSelect";
	public $description = "";
	public $value       = "";
	public $size        = "20";
	public $name        = "";
	public $style       = "";
	public $title       = "";
	public $class       = "";
	public $readonly    = "";
	public $disabled    = "";
	public $accesskey   = "";
	
	//Events
	public $onfocus;
	public $onblur;
	public $onselect;
	public $onchange;
	public $onclick;
	public $ondblclick;
	public $onmousedown;
	public $onmouseup;
	public $onmouseover;
	public $onmousemove;
	public $onmouseout;
	public $onkeypress;
	public $onkeydown;
	public $onkeyup;
	
	//Data
	private $data =  array();
	protected $render = "";
	
	public function show(){
	
		$this->parseFromFile($this->templatePath."/".$this->templateFile);
		
		if(empty($this->name)){
			$this->name=$this->oid;
		}
		
		if(!empty($this->list)){
			$this->setListOfValues(explode($this->separator,$this->list));
			
		}
		
		if(!empty($this->file)){
			$this->loadFromFile($this->file,$this->separator);
		}
		
		
		
		$this->events();
		$this->optionsRender();
		
		return $this->replace();
	}
	
	private function events(){
	
		$events = array(
			"onfocus","onblur","onselect","onchange","onclick","ondblclick"	
			,"onmousedown","onmouseup","onmouseover","onmousemove","onmouseout"
			,"onkeypress","onkeydown","onkeyup"
		);
		
		$out="";
		foreach($events as $name){
			if(isset($this->$name) && !empty($this->$name)){
				
				$this->event->name  = $name;
				$this->event->value = $this->$name;
				$out.= $this->event->replace();
			}else{
				$out.="";
			}
		}
		
		$this->event->block=$out;
	}
	
	public function addOption($value, $option=""){
		if(isset($this->options)){
				$this->options->value  = $value;
				$this->options->option = $option;
				if($this->value==$value){
					$this->options->selected="selected='selected'";
				}else{
					$this->options->selected="";
				}
				$this->render.= $this->options->replace();
		}
	}
	
	public function setListOfValues($list){
			foreach($list as $value){
			if(isset($this->options)){
				$this->addOption($value,$value);
			}
		}
		unset($data);
	}
	
	public function loadFromArray(array $data){
		foreach($data as $value => $option){
			if(isset($this->options)){
				$this->addOption($value,$option);
			}
		}
		unset($data);
	}
	
	public function loadFromFile($path,$separator=","){
		if(file_exists($path)){
			$file=file($path);
			foreach($file as $row){
				list($value,$option) = explode($separator,$row);
				$value  = trim($value);
				$option = trim($option);
				$this->addOption($value,$option);
			}
			unset($file);
		}
	}
	
	private function optionsRender(){
		if(isset($this->options))
		$this->options->block = $this->render;
		
	}
	
}

?>